//
//  HttpTool.swift
//  newToDay
//
//  Created by 中创 on 2019/11/29.
//  Copyright © 2019 LS. All rights reserved.
//

import Foundation

import Alamofire

private let httpShareInstance = HttpTool()

enum MethodType {
    case get
    case post
}

class HttpTool: NSObject {
    class var shareInstance: HttpTool {
        return httpShareInstance
    }
}

extension HttpTool{
    
    func requestData(_ type : MethodType, URLString:String, parameters:[String:Any], finishCallBack : @escaping (_ response : Any) -> ()) {
        
        let method = type == .get ? HTTPMethod.get : HTTPMethod.post
        
        Alamofire.request(URLString, method: method, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (responsejson) in
            
            guard responsejson.result.value != nil else{
                print(responsejson.result.error)
                return
            }
            guard responsejson.result.isSuccess else{
                return
            }
            if let value = responsejson.result.value{
                finishCallBack(value)
            }
        }
    }
}
